package cn.com.css.taxInspection.system.controller;

import java.util.List;

import javax.annotation.Resource;

import com.baomidou.dynamic.datasource.annotation.DS;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.com.css.taxInspection.common.result.DataResult;
import cn.com.css.taxInspection.system.entity.SysFilesEntity;
import cn.com.css.taxInspection.system.service.SysFilesService;
import cn.com.css.taxInspection.util.TsBqUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 文件上传
 *
 * @author wenbin
 * @version V1.0
 * @date 2020年3月18日
 */
@RestController
@RequestMapping("/sysFiles")
@Api(tags = "文件管理")
public class SysFilesController {
	@Resource
	private SysFilesService sysFilesService;

	/**
	 * 新增
	 */
	@ApiOperation(value = "新增")
	@PostMapping("/upload")
    public DataResult add(@RequestParam(value = "file") MultipartFile file) {
		// 判断文件是否空
		if (file == null || file.getOriginalFilename() == null
				|| "".equalsIgnoreCase(file.getOriginalFilename().trim())) {
			return DataResult.fail("文件为空");
		}

		return sysFilesService.saveFile(file);
	}

	/**
	 * 删除
	 */
	@ApiOperation(value = "删除")
	@DeleteMapping("/delete")
	@RequiresPermissions("sysFiles:delete")
    public DataResult delete(@RequestBody @ApiParam(value = "id集合") List<String> ids) {
		sysFilesService.removeByIdsAndFiles(ids);
		return DataResult.success();
	}

	/**
	 * 查询分页数据
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@ApiOperation(value = "查询分页数据")
	@PostMapping("/listByPage")
	@RequiresPermissions("sysFiles:list")
    public DataResult findListByPage(@RequestBody SysFilesEntity sysFiles) {
		Page page = new Page(sysFiles.getPage(), sysFiles.getLimit());
		IPage<SysFilesEntity> iPage = sysFilesService.page(page,
				Wrappers.<SysFilesEntity>lambdaQuery().orderByDesc(SysFilesEntity::getCreateDate));
		return DataResult.success(iPage);
	}

	/**
	 * 图片转base64
	 */
	@ApiOperation(value = "图片转base64")
	@PostMapping("/imageToBase64")
    public DataResult imageToBase64(@RequestParam(value = "file") MultipartFile file) {
		// 判断文件是否空
		if (file == null || file.getOriginalFilename() == null
				|| "".equalsIgnoreCase(file.getOriginalFilename().trim())) {
			return DataResult.fail("文件为空");
		}

		String imageBase64 = "data:image/" + TsBqUtils.getFileType(file.getOriginalFilename()) + ";base64,"
				+ TsBqUtils.imageToBase64Str(file);

		return DataResult.success(imageBase64);
	}

}
